package CustomUI;

import org.Consts;

import com.example.dworld3.Drawings;
import com.ydev.dworld3.R;

import DTO.DrawingDTO;
import GameTools.ydPoint;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ydDrawingCell extends LinearLayout {

	private Context mContext;
	private LayoutInflater layoutInflater;

	public LinearLayout llMain;
	ImageView imgPicture;
	View bar;
	ImageView imgcheck;
	LinearLayout llHolder;

	ydOnClickListeners listeners = new ydOnClickListeners();
	ydOnClickLongListeners longListeners = new ydOnClickLongListeners();

	boolean selected = false;
	int id=-1;

	public ydDrawingCell(Context context) {
		super(context);
		this.mContext = context;
		inflate();
		bindViews();
		init();
	}


	public ydDrawingCell(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;

	}

	private void inflate() {
		layoutInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.drawing_cell, this, true);
	}

	private void bindViews() {
		llMain = (LinearLayout) findViewById(R.id.llDrawingMain);
		imgPicture = (ImageView) findViewById(R.id.imgSmallPicture);
		llHolder=(LinearLayout)findViewById(R.id.llHolder);
		bar = (View) findViewById(R.id.view1);
		imgcheck = (ImageView) findViewById(R.id.imgCheckbox);
	}

	public void init() {
		this.setOnClickListener(listeners);
		this.setOnLongClickListener(longListeners);

		listeners.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

			}

		});
	}
	
	public void loadDrawing(int id){
		ProgressDialog progress;
		progress = new ProgressDialog(mContext);
		progress.setMessage("Loading...");
		progress.setIndeterminate(false);
		progress.setCancelable(false);
		progress.show();
	}

	public void setCellSelected(boolean b) {
		if (b) {
			imgcheck.setVisibility(View.VISIBLE);
			selected = b;
		} else {
			imgcheck.setVisibility(View.GONE);
			selected = b;
		}
	}
	
	/*public void setOnClick(OnClickListener c){
		this.setOnClick(c);
	}*/

	public void setImage(Bitmap img) {

		imgPicture.setImageBitmap(img);
	}
	
	/*public void setLayer(Bitmap b) {
		llHolder.removeAllViews();
		llHolder.addView(v);
	}*/

	public void addOnClickListener(OnClickListener onClickListener) {
		listeners.addOnClickListener(onClickListener);

	}

	public void setSize(int x, int y) {
		android.view.ViewGroup.LayoutParams par = llMain.getLayoutParams();// =
																			// new
		// LinearLayout.LayoutParams(
		if (par == null)
			par = new LayoutParams(x, y);
		par.width = x;
		par.height = y;
		llMain.setLayoutParams(par);
		// 50, 50);
		// par.setMargins(10, 0, 0, 10);
	}

	public void addToLeftMargin(int i) {
		LayoutParams par = (LayoutParams) llMain.getLayoutParams();
		par.leftMargin+=i*5;
		llMain.setLayoutParams(par);

	}

	public void setBarColor(int layer) {
		bar.setVisibility(View.VISIBLE);
		bar.setBackgroundColor(Color.parseColor(Consts.colorsList[layer%Consts.colorsList.length]));
		
	}

	public void setLayerID(int currentLayer) {
		id=currentLayer;
		
	}

	public int getlID() {
		return id;
	}


	public void getDrawingID(int i) {
		//new getDrawingAsync().execute(this, i);
		Bitmap b =DrawingDTO.getCurrentDrawing(i);
		setImage(b);
		
	}


}

class getDrawingAsync extends AsyncTask<Object, Void, Boolean> {
	@Override
	protected Boolean doInBackground(Object... objs) {
		ydDrawingCell dc=(ydDrawingCell)objs[0];
		Bitmap b =DrawingDTO.getCurrentDrawing((Integer)objs[1]);
		dc.setImage(Bitmap.createBitmap(b));
		return null;

	}

	protected void onPostExecute(Double result) {

	}

	protected void onProgressUpdate(Integer... progress) {

	}
}